package view;

import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import action.Action;

import javax.swing.JProgressBar;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class FetchDataMes extends JDialog {

	static private JPanel contentPanel = new JPanel();
	static private JProgressBar progressBar;
	static private JLabel label; // 爬取状态提示标签
	static private JPanel panel; // 爬取完成后显示的面板
	/**
	 * 显示对话框
	 */
	public static void showMesDialog() {
		try {
			FetchDataMes dialog = new FetchDataMes();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public FetchDataMes() {
		setBounds(100, 100, 450, 300);
		getContentPane().setLayout(null);
		contentPanel.setBounds(0, 0, 434, 34);
		contentPanel.setLayout(new FlowLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel);
		{
			progressBar = new JProgressBar(0,100);
			progressBar.setBounds(67, 25, 295, 78);
			getContentPane().add(progressBar);
		}
		{
			label = new JLabel("正在爬取...");
			label.setFont(new Font("SimSun", Font.PLAIN, 18));
			label.setBounds(160, 96, 223, 45);
			getContentPane().add(label);
		}
		
		panel = new JPanel();
		panel.setVisible(false);//一开始设置不可见
		panel.setBounds(25, 165, 374, 86);
		getContentPane().add(panel);
		panel.setLayout(null);
		
		JButton btnNewButton = new JButton("立即导入数据库中");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Action.pushIntoDBFromLocalFile();
				FetchDataMes.this.dispose();
			}
		});
		btnNewButton.setFont(new Font("SimSun", Font.PLAIN, 17));
		btnNewButton.setBounds(10, 10, 188, 66);
		panel.add(btnNewButton);
		
		JButton button = new JButton("稍后导入");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				FetchDataMes.this.dispose();
			}
		});
		button.setFont(new Font("SimSun", Font.PLAIN, 17));
		button.setBounds(223, 10, 141, 66);
		panel.add(button);
	}
	
	
	/**
	 * 响应爬取完成后, 改变label内容并显示panel 
	 * @author 胡伟
	 *
	 */
	public static void setLabelAndPanel(boolean ifFinished) {
		label.setText("已完成爬取");
		
		panel.setVisible(true);
		contentPanel.repaint();
	}
	
	
	/**
	 * 刷新进度条
	 * @author 胡伟
	 *
	 */
	public static void setProgressBar(int value,boolean ifFinised) {
		if(ifFinised) {
			progressBar.setValue(progressBar.getMaximum());
		}else {
			progressBar.setValue(value);
		}
		contentPanel.repaint();
	}

}
